package com.wh.testThreadPool;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//Executors 工具类
//使用了线程池之后，运用线程池来创建线程
public class ThreadPoolDemo {
    public static void main(String[] args) {
        int i1 = Runtime.getRuntime().availableProcessors();
        System.out.println("本机核树："+Runtime.getRuntime().availableProcessors());
        ExecutorService executorService = Executors.newSingleThreadExecutor();//单个线程
        ExecutorService executorService1 =Executors.newFixedThreadPool(5);//创建一个固定大小的
        ExecutorService executorService2 = Executors.newCachedThreadPool();//可伸缩的，遇强则强

        try{
            for (int i = 0; i < 10; i++) {
                executorService2.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"--->ok");
                });
            }
        }catch (Exception e){
         e.printStackTrace();
        }finally {
            //线程池用完，程序结束，关闭线程
            executorService2.shutdown();
        }


    }
}
